#!/usr/bin/env python3
from gi.repository import GLib

import dbus
import dbus.service
import os
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)


OPATH = "/org/ucleaner"
IFACE = "org.ucleaner"
BUS_NAME = "org.dbus.ucleaner"

class Ucleaner(dbus.service.Object):
    def __init__(self):
        bus = dbus.SystemBus()
        bus.request_name(BUS_NAME)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)

    @dbus.service.method(IFACE, out_signature='s')
    def clean_apt_cache(self):
        dir_name = "/var/cache/apt/archives"
        test_dir = os.listdir(dir_name)
        for item in test_dir:
            if item.endswith(".deb"):
                os.remove(os.path.join(dir_name, item))
        return ""
    @dbus.service.method(IFACE, out_signature='s')
    def clean_logs(self):
        os.system("/usr/bin/log-c.sh")
        return ""
    @dbus.service.method(IFACE, out_signature='s')
    def remove_unused_packages(self):
        os.system("apt-get purge autoremove -y")
        return ""

if __name__ == "__main__":
    a = Ucleaner()
    loop = GLib.MainLoop()

    loop.run()
